package com.wzqj.agent.web.service.impl;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wzqj.agent.util.Util;
import com.wzqj.agent.web.service.BizClassifyService;
@Service("bizClassifyService")
public class BizClassifyServiceImpl implements BizClassifyService {

	@Override
	public JSONArray childClassify(Integer id, String childClassify,Integer level) {
		//获取全部信息
		String s = Util.readTxt("res/bizClassify.json");
        JSONArray bizClassify = JSONArray.parseArray(s);
        //如果级别为一级
        if (1==level) {
	        for(int i = 0; i < bizClassify.size(); i++){
				//获取一级行业
				JSONObject level1 = bizClassify.getJSONObject(i);
				
				Integer level1Id = level1.getInteger("id");
				String level1Name = level1.getString("name");
				String level1Level = level1.getString("level");
				JSONArray childClassify1 = JSONArray.parseArray(level1.getString("childClassify"));
				//判断是否为一级行业
				if (id==level1Id) {
					if (childClassify1!=null&&childClassify1.size()>0) {
						return childClassify1;//返回二级行业
					}
				}
			}
		}
        //如果级别为二级
        if (2==level) {
        	for(int i = 0; i < bizClassify.size(); i++){
        		//获取一级行业
        		JSONObject level1 = bizClassify.getJSONObject(i);
        		
        		Integer level1Id = level1.getInteger("id");
        		String level1Name = level1.getString("name");
        		String level1Level = level1.getString("level");
        		JSONArray childClassify1 = JSONArray.parseArray(level1.getString("childClassify"));
        		//判断二级是否为空
        		if (childClassify1!=null&&childClassify1.size()>0) {//不为空
        			for (int j = 0; j < childClassify1.size(); j++) {
        				//获取二级信息
        				JSONObject level2 = childClassify1.getJSONObject(j);
        				Integer level2Id = level2.getInteger("id");
        				String level2Name = level2.getString("name");
        				String level2Level = level2.getString("level");
        				JSONArray childClassify2 = JSONArray.parseArray(level2.getString("childClassify"));
        				if (level2Id.equals(id)) {
        					//判断三级是否为空
        					if (childClassify2!=null&&childClassify2.size()>0) {//不为空
        						return childClassify2;
        					}
						}
        			}
        		}
        	}
			
		}
        return null;

	}
}
